package com.chefu.operator.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.common.exception.pojo.LyException;
import com.chefu.common.pojo.PageResult;
import com.chefu.operator.mapper.ArticleRecommendMapper;
import com.chefu.operator.mapper.PartsCategoyMapper;
import com.chefu.operator.pojo.ArticleRecommend;
import com.chefu.operator.pojo.PartsCategory;
import com.chefu.operator.vo.RecommendListVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RecommendService {

    @Autowired(required = false)
    private ArticleRecommendMapper articleRecommendMapper;

    @Autowired(required = false)
    private PartsCategoyMapper partsCategoyMapper;

    /**
     * 查询推文列表方法
     */

    public PageResult<RecommendListVo> queryRecommendListByPage(Long page,
                                                                Integer rows,
                                                                Map<String, Object> paramMap) {
        //1.创建查询条件对象
        QueryWrapper<ArticleRecommend> queryWrapper = new QueryWrapper<>();
        String name = (String) paramMap.get("name");
        String cid1Str = (String) paramMap.get("cid1");
        String cid2Str = (String) paramMap.get("cid2");
        String cid3Str = (String) paramMap.get("cid3");
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.eq("name", name);//设置查询条件name
        }
        if (cid1Str != null && !cid1Str.equals("")) {
            Long cid1 = Long.parseLong(cid1Str);
            queryWrapper.eq("cid1", cid1);
        }
        if (cid2Str != null && !cid2Str.equals("")) {
            Long cid2 = Long.parseLong(cid2Str);
            queryWrapper.eq("cid2", cid2);
        }
        if (cid3Str != null && !cid3Str.equals("")) {
            Long cid3 = Long.parseLong(cid3Str);
            queryWrapper.eq("cid3", cid3);
        }
        IPage<ArticleRecommend> iPage = new Page<>(page, rows);
        IPage<ArticleRecommend> articleRecommendIPage = articleRecommendMapper.selectPage(iPage, queryWrapper);
        //2.封装RecommendListVo对象
        PageResult<RecommendListVo> pageResult = new PageResult<>();
        if(articleRecommendIPage.getRecords()!=null){
            List<RecommendListVo> recommendListVoList = articleRecommendIPage.getRecords().stream().map(articleRecommend -> {
                RecommendListVo recommendListVo = new RecommendListVo();
                recommendListVo.setName(articleRecommend.getName());
                recommendListVo.setId(articleRecommend.getId());
                //根据id查询partsCategory表
                PartsCategory partsCategory = partsCategoyMapper.selectById(articleRecommend.getCid1());
                recommendListVo.setCid1(partsCategory.getName());
                return recommendListVo;
            }).collect(Collectors.toList());
            //3.封装PageResult对象
            pageResult.setTotalPage((int) articleRecommendIPage.getPages());
            pageResult.setTotal((int) articleRecommendIPage.getTotal());
            pageResult.setItems(recommendListVoList);
        }
        //3.返回
        return pageResult;
    }

    /**
     * 新增推文方法
     */
    public String addArticleRecommend(ArticleRecommend articleRecommend) {
        //1.判断推文名称是否为空
        if (StringUtils.isEmpty(articleRecommend.getName())) {
            throw new LyException(804, "推文名字不能为空");
        }
        //2.判断cid1,cid2,cid3是否为空
        if (articleRecommend.getCid1() == null
                || articleRecommend.getCid2() == null
                || articleRecommend.getCid3() == null) {
            throw new LyException(805,"推文分类不能为空");
        }
        //3.插入数据库
        articleRecommendMapper.insert(articleRecommend);
        return "新增推文成功";
    }

    /**
     * 根据id查询推文详情
     */
    public ArticleRecommend queryArticleRecommendById(Long id) {
        //根据传入id查询数据库
        ArticleRecommend articleRecommend = articleRecommendMapper.selectById(id);
        return articleRecommend;
    }

    /**
     *修改推文
     */
    public String updateArticleRecommend(ArticleRecommend articleRecommend) {
        //1.判断推文名称是否为空
        if (StringUtils.isEmpty(articleRecommend.getName())) {
            throw new LyException(804, "推文名字不能为空");
        }
        //2.判断cid1,cid2,cid3是否为空
        if (articleRecommend.getCid1() == null
                || articleRecommend.getCid2() == null
                || articleRecommend.getCid3() == null) {
            throw new LyException(805,"推文分类不能为空");
        }
        //3.更新数据库
        articleRecommendMapper.updateById(articleRecommend);
        return "修改推文成功";
    }

    /**
     *删除推文方法
     */
    public void delArticleRecomend(Long id) {
        try {
            articleRecommendMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LyException(820,"删除推文失败");
        }
    }
}
